package com.nsgf.library.photopicker.widget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.IntDef;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.nsgf.library.data.api.APIs;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.matisse.MimeType;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
 * Created by GC on 2024/9/15 0015.
 */
public class MultiPickResultView extends FrameLayout {

    public ArrayList<WorkOrderDetailBean.FileDto> getPhotos() {
        return selectedPhotos;
    }


    @IntDef({ACTION_SELECT, ACTION_ONLY_SHOW})

    //Tell the compiler not to store annotation data in the .class file
    @Retention(RetentionPolicy.SOURCE)

    //Declare the NavigationMode annotation
    public @interface MultiPicAction {
    }

    public static final int ACTION_SELECT = 1;//该组件用于图片选择
    public static final int ACTION_ONLY_SHOW = 2;//该组件仅用于图片显示

    private int action;

    private int maxCount;

    RecyclerView recyclerView;
    public PhotoAdapter photoAdapter;
    public ArrayList<WorkOrderDetailBean.FileDto> selectedPhotos;

    public MultiPickResultView(Context context) {
        this(context, null, 0);
    }

    public MultiPickResultView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MultiPickResultView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
        initData(context, attrs);
        initEvent(context, attrs);
    }

    private void initEvent(Context context, AttributeSet attrs) {

    }

    private void initData(Context context, AttributeSet attrs) {

    }

    private void initView(Context context, AttributeSet attrs) {

        recyclerView = new RecyclerView(context, attrs);
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MultiPickResultView(Context context, AttributeSet attrs, int defStyleAttr, String defStyleRes) {
        this(context, attrs, defStyleAttr);
    }


    public void init(Activity context, @MultiPicAction int action,
                     ArrayList<WorkOrderDetailBean.FileDto> photos,
                     int size,
                     int index,
                     Set<MimeType> mimeTypes,
                     PhotoAdapter.PhotoOnClick photoOnClick) {
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        this.addView(recyclerView);
        this.action = action;
        if (action == MultiPickResultView.ACTION_ONLY_SHOW) {//当只用作显示图片时,一行显示3张
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        }

        selectedPhotos = new ArrayList<>();

        this.action = action;
        if (photos != null && photos.size() > 0) {
            selectedPhotos.addAll(photos);
        }
        for (int i = 0; i < selectedPhotos.size(); i++) {
            if (!selectedPhotos.get(i).getFilePath().startsWith("http")) {
                selectedPhotos.get(i).setFilePath(APIs.BASE_URL_IMG + selectedPhotos.get(i).getFilePath());
            }
        }
        photoAdapter = new PhotoAdapter(context, selectedPhotos, size, index, mimeTypes, photoOnClick);
        photoAdapter.setAction(action);
        recyclerView.setAdapter(photoAdapter);
        //recyclerView.setLayoutFrozen(true);
    }

    public void init(Activity context, @MultiPicAction int action, ArrayList<WorkOrderDetailBean.FileDto> photos, int size) {
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        this.addView(recyclerView);
        this.action = action;
        if (action == MultiPickResultView.ACTION_ONLY_SHOW) {//当只用作显示图片时,一行显示3张
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        }

        selectedPhotos = new ArrayList<>();

        this.action = action;
        if (photos != null && photos.size() > 0) {
            selectedPhotos.addAll(photos);
        }
        for (int i = 0; i < selectedPhotos.size(); i++) {
            if (!selectedPhotos.get(i).getFilePath().startsWith("http")) {
                selectedPhotos.get(i).setFilePath(APIs.BASE_URL_IMG + selectedPhotos.get(i).getFilePath());
            }
        }
        photoAdapter = new PhotoAdapter(context, selectedPhotos, size);
        photoAdapter.setAction(action);
        recyclerView.setAdapter(photoAdapter);
        //recyclerView.setLayoutFrozen(true);
    }

    public void init(Context context, @MultiPicAction int action, ArrayList<WorkOrderDetailBean.FileDto> photos, int size) {

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(size, OrientationHelper.VERTICAL));
        this.addView(recyclerView);
        this.action = action;
        if (action == MultiPickResultView.ACTION_ONLY_SHOW || action == 3) {//当只用作显示图片时,一行显示3张
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(size, OrientationHelper.VERTICAL));
        }

        selectedPhotos = new ArrayList<>();

        this.action = action;
        if (photos != null && photos.size() > 0) {
            selectedPhotos.addAll(photos);
        }
        photoAdapter = new PhotoAdapter(context, selectedPhotos);
        photoAdapter.setAction(action);
        recyclerView.setAdapter(photoAdapter);
        //recyclerView.setLayoutFrozen(true);
    }

    public void init(Context context, @MultiPicAction int action, ArrayList<WorkOrderDetailBean.FileDto> photos, int size, int drawable) {

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        this.addView(recyclerView);
        this.action = action;
        if (action == MultiPickResultView.ACTION_ONLY_SHOW || action == 3) {//当只用作显示图片时,一行显示3张
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        }

        selectedPhotos = new ArrayList<>();

        this.action = action;
        if (photos != null && photos.size() > 0) {
            selectedPhotos.addAll(photos);
        }
        for (int i = 0; i < selectedPhotos.size(); i++) {
            if (!selectedPhotos.get(i).getFilePath().startsWith("http")) {
                selectedPhotos.get(i).setFilePath(APIs.BASE_URL_IMG + selectedPhotos.get(i).getFilePath());
            }
        }
        photoAdapter = new PhotoAdapter(context, selectedPhotos, size, drawable);
        photoAdapter.setAction(action);
        recyclerView.setAdapter(photoAdapter);
        //recyclerView.setLayoutFrozen(true);
    }

    public void init(Context context, @MultiPicAction int action, ArrayList<WorkOrderDetailBean.FileDto> photos, int size, int drawable, PhotoAdapter.PhotoOnClick photoOnClick) {

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        this.addView(recyclerView);
        this.action = action;
        if (action == MultiPickResultView.ACTION_ONLY_SHOW || action == 3) {//当只用作显示图片时,一行显示3张
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        }

        selectedPhotos = new ArrayList<>();

        this.action = action;
        if (photos != null && photos.size() > 0) {
            selectedPhotos.addAll(photos);
        }
        photoAdapter = new PhotoAdapter(context, selectedPhotos, size, drawable, photoOnClick);
        photoAdapter.setAction(action);
        recyclerView.setAdapter(photoAdapter);
        //recyclerView.setLayoutFrozen(true);
    }

    public void init(Context context, @MultiPicAction int action, ArrayList<WorkOrderDetailBean.FileDto> photos, boolean picAction) {

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        this.addView(recyclerView);
        this.action = action;
        if (action == MultiPickResultView.ACTION_ONLY_SHOW || action == 3) {//当只用作显示图片时,一行显示3张
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        }

        selectedPhotos = new ArrayList<>();

        this.action = action;
        if (photos != null && photos.size() > 0) {
            selectedPhotos.addAll(photos);
        }
        photoAdapter = new PhotoAdapter(context, selectedPhotos);
        photoAdapter.setAction(action);
        recyclerView.setAdapter(photoAdapter);
        //recyclerView.setLayoutFrozen(true);
    }

    private boolean isNumber;

    public void init(Context context, @MultiPicAction int action, ArrayList<WorkOrderDetailBean.FileDto> photos, int size, boolean isNumber) {

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        this.addView(recyclerView);
        this.action = action;
        if (action == MultiPickResultView.ACTION_ONLY_SHOW || action == 3) {//当只用作显示图片时,一行显示3张
            recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
        }
        selectedPhotos = new ArrayList<>();
        this.action = action;
        if (photos != null && photos.size() > 0) {
            selectedPhotos.addAll(photos);
        }
        photoAdapter = new PhotoAdapter(context, selectedPhotos);
        photoAdapter.setAction(action);
        recyclerView.setAdapter(photoAdapter);
    }

    public void showPics(List<WorkOrderDetailBean.FileDto> paths) {
        if (paths != null) {
            selectedPhotos.clear();
            selectedPhotos.addAll(paths);
            photoAdapter.notifyDataSetChanged();
        }

    }

    public ArrayList<WorkOrderDetailBean.FileDto> getPhotoPaths() {
        return selectedPhotos;
    }

    public List<WorkOrderDetailBean.FileDto> getLiveImageListSubmit() {
        if (null == getPhotoPaths())
            return null;
        List<WorkOrderDetailBean.FileDto> fileDtos = getPhotoPaths();
        for (int i = 0; i < fileDtos.size(); i++) {
            fileDtos.get(i).setFilePath(fileDtos.get(i).getFilePath().replace(APIs.BASE_URL_IMG, ""));
        }
        return fileDtos;
    }


    public int getPickIndex() {
        return photoAdapter.getPickIndex();
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (action == ACTION_SELECT) {
            PhotoPickUtils.onActivityResult(requestCode, resultCode, data, new PhotoPickUtils.PickHandler() {
                @Override
                public void onPickSuccess(ArrayList<WorkOrderDetailBean.FileDto> photos) {
                    photoAdapter.refresh(photos);
                }

                @Override
                public void onPreviewBack(ArrayList<WorkOrderDetailBean.FileDto> photos) {
                    photoAdapter.refresh(photos);
                }

                @Override
                public void onPickFail(String error) {
                    Toast.makeText(getContext(), error, Toast.LENGTH_LONG).show();
                    selectedPhotos.clear();
                    photoAdapter.notifyDataSetChanged();
                }

                @Override
                public void onPickCancle() {
                    //Toast.makeText(getContext(),"取消选择",Toast.LENGTH_LONG).show();
                }
            });
        }

    }

}
